﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.AgentEditor.Controls
{

	/// <summary>
	/// Declaration of BankAccountTabCtrl class
	/// The bank account tab include the agent details control
	/// on the first page, and bank accounts on the following tabs
	/// </summary>
	public partial class BankAccountTabCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public BankAccountTabCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		protected void ctrlBankAccount_DeleteButtonClicked(object sender, EventArgs e)
		{

			// Update the heading for the saved tab
			BankAccountCtrl bankAccountCtrl = sender as BankAccountCtrl;
			TabItem tabItem = bankAccountCtrl.FindParent(typeof(TabItem)) as TabItem;
			int iIndex = tabCtrl.Items.IndexOf(tabItem);

			// Remove this tab
			_removeTab(iIndex);

		}

		void bankAccountCtrl_ContentChanged(object sender, ChildCtrlContentChangeEventArg e)
		{

			// Find the tab item
			TabItem tabItem = (sender as FrameworkElement).FindParent(typeof(TabItem)) as TabItem;

			// Is this a new tab
			bool bNewTab = (tabCtrl.Items.IndexOf(tabItem) == (tabCtrl.Items.Count - 1));

			// If this is the new tab...
			if (bNewTab)
			{

				// Add another new tab
				_updateTabHeading(_addTab(true));

			}

			// Get the contact control
			BankAccountCtrl bankAccountCtrl = tabItem.Content as BankAccountCtrl;

			// Compose a heading
			string strTabHeading = bankAccountCtrl.ComposeTabHeading();

			// Update heading and buttons
			if ((bNewTab) || (e.UpdateTabHeading))
				_updateTabHeading(tabItem);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private TabItem _addTab(bool bNewTab)
		{

			// Declare return variable
			TabItem Ftmp = null;

			// Create a new tab
			Ftmp = new TabItem();

			// Add a bank account
			BankAccountCtrl bankAccountCtrl = new BankAccountCtrl();
			bankAccountCtrl.DeleteButtonClicked += new EventHandler(ctrlBankAccount_DeleteButtonClicked);
			bankAccountCtrl.ContentChanged += new EventHandler<ChildCtrlContentChangeEventArg>(bankAccountCtrl_ContentChanged);
			Ftmp.Content = bankAccountCtrl;

			// Add a tab header
			TabHeader tabHeader = new TabHeader();
			Ftmp.Header = tabHeader;

			// Add to tab control
			if (bNewTab)
				tabCtrl.Items.Add(Ftmp);
			else
				tabCtrl.Items.Insert(tabCtrl.Items.Count - 1, Ftmp);

			// Inform all tabs of index change
			for (int i1 = 1; i1 < tabCtrl.Items.Count; i1++)
				((tabCtrl.Items[i1] as TabItem).Content as BankAccountCtrl).OnTabIndexChange();

			// Return variable
			return Ftmp;

		}

		private void _removeTab(int iOffset)
		{

			// Remove tab-item from control
			tabCtrl.Items.RemoveAt(iOffset);

			// Inform all tabs of index change
			for (int i1 = 1; i1 < tabCtrl.Items.Count; i1++)
				((tabCtrl.Items[i1] as TabItem).Content as BankAccountCtrl).OnTabIndexChange();

		}

		private void _updateTabHeading(TabItem tabItem)
		{

			// Get the control
			BankAccountCtrl ctrlBankAccount = tabItem.Content as BankAccountCtrl;
#if DEBUG
			Debug.Assert(ctrlBankAccount != null);
#endif // #if DEBUG

			// Compose label
			string strLabel;
			int iIndex = tabCtrl.Items.IndexOf(tabItem);
			if (iIndex == (tabCtrl.Items.Count - 1))
				strLabel = Definitions.NewTabHeading;
			else
				strLabel = ctrlBankAccount.ComposeTabHeading();

			// Set label
			(tabItem.Header as TabHeader).Label = strLabel.DotDotDot(14);

		}

		public void PostSupportDataLoaded()
		{

			// Called after plug-in has loaded support data

			// Add the new bank account
			if (tabCtrl.Items.Count == 1)
				_updateTabHeading(_addTab(true));

		}

		public void Set(AgentTransport agentTransport, bool bReloading)
		{

			// Set controls
			agentDetailsCtrl.Set(agentTransport);

			// If need to add tabs...
			if (tabCtrl.Items.Count < (agentTransport.BankAccounts.Count + 2))
			{

				// Add bank accounts
				int iNewTabsAdded = agentTransport.BankAccounts.Count - tabCtrl.Items.Count + 2;
				while (iNewTabsAdded > 0)
				{
					_addTab(false);
					iNewTabsAdded--;
				} // while-loop

			}
			else if ((agentTransport.BankAccounts.Count + 2) < tabCtrl.Items.Count)
			{

				// Remove bank accounts
				int iRemoveTabs = tabCtrl.Items.Count - agentTransport.BankAccounts.Count - 2;
				while (iRemoveTabs > 0)
				{
					_removeTab(tabCtrl.Items.Count - 2);
					iRemoveTabs--;
				} // while-loop

			}

			// Loop through the bank accounts
#if DEBUG
			Debug.Assert(tabCtrl.Items.Count == (agentTransport.BankAccounts.Count + 2));
#endif // #if DEBUG
			for (int i1 = 0; i1 < agentTransport.BankAccounts.Count; i1++)
			{

				// Get the contact control
				TabItem tabItem = tabCtrl.Items[i1+1] as TabItem;
				BankAccountCtrl bankAccountCtrl = tabItem.Content as BankAccountCtrl;

				// Set controls
				bankAccountCtrl.Set(agentTransport.BankAccounts[i1]);

				// Update the tab item heading
				_updateTabHeading(tabCtrl.Items[i1+1] as TabItem);

			}

			// Select the first tab
			if (!bReloading)
				tabCtrl.SelectedIndex = 0;

		}

		public void Get(AgentTransport agentTransport, List<string> Errors)
		{

			// Populate the agent object with details from the control
			// and find errors in the user input

			// Set controls
			agentDetailsCtrl.Get(agentTransport, Errors);

			// Load the bank accounts
			for (int iTabIndex = 1; iTabIndex < tabCtrl.Items.Count-1; iTabIndex++)
			{

				// Get the bank account control
				BankAccountCtrl bankAccountCtrl = (tabCtrl.Items[iTabIndex] as TabItem).Content as BankAccountCtrl;
				BankAccountTransport newBankAccountHeading = new BankAccountTransport();
				bankAccountCtrl.Get( newBankAccountHeading, Errors );
				agentTransport.BankAccounts.Add(newBankAccountHeading);

			}

		}

	}

}
